home *** CD-ROM | disk | FTP | other *** search
/ Programmer Plus 2007 / Programmer-Plus-2007.iso / Programming / XML Utilities / Professional Programmer XSL IDE / Xselerator25.msi / Data.Cab / F23800_FixedLengthTextToHTML.xsl < prev    next >
Encoding:
Extensible Markup Language  |  2001-10-05  |  4.7 KB  |  115 lines

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!-- ===========================================================
  3.   Category:       NonXML
  4.   Sub-category:   FixedLengthText
  5.   Author:         David Silverlight
  6.                   HeadGeek@xmlpitstop.com
  7.   Created:        2001-05-16
  8.   Description:-
  9.     This stylsheet demonstrates transforming a fixed length text
  10.     file as an HTML table.  In this example we are using
  11.     substring functions to  create an HTML document from a text
  12.     file based on the start and end positions of the data.
  13.     Additionally, we are calling a template named TrimTrailing
  14.     to remove any extra traling spaces from our table.
  15. ================================================================ -->
  16. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  17.   <xsl:output method="html" />
  18.  
  19.   <xsl:template match="/">
  20.     <html>
  21.       <head>
  22.         <style type="text/css"><![CDATA[
  23.         H1 {COLOR: red; FONT-FAMILY: Arial; FONT-SIZE: 14pt;}
  24.         H2 {COLOR: darkblue; FONT-FAMILY: Arial; FONT-SIZE: 12pt;}
  25.         .head {COLOR: darkblue; FONT-FAMILY: Arial; FONT-SIZE: 14pt;}
  26.         .subhead {COLOR: darkblue; FONT-FAMILY: Arial; FONT-SIZE: 12pt;}
  27.         .text {COLOR: black; FONT-FAMILY: Arial; FONT-SIZE: 12pt;}
  28.         TH {COLOR: white; FONT-FAMILY: Arial; background-color: darkblue;}
  29.         TD {COLOR: darkblue; FONT-FAMILY: Arial}
  30.         TR { background-color: beige; }
  31.         BODY { background-color: beige; }
  32.         ]]></style>
  33.       </head>
  34.       <body>
  35.         <xsl:for-each select="*">
  36.           <xsl:variable name="allContents" select="." />
  37.  
  38.           <!--In this template, I load the file into a variable
  39.             so that it can be handled as a large string.  Using the string functions like substring, it is easy to parse it given the startpositions and lengths  of the elements -->
  40.           <table border="1">
  41.             <tr>
  42.               <th>ID</th>
  43.               <th>Title</th>
  44.               <th>Name</th>
  45.               <th>Phone</th>
  46.             </tr>
  47.  
  48.             <xsl:call-template name="SplitFixedLengthToHTML">
  49.               <xsl:with-param name="strInput" select="$allContents" />
  50.               <xsl:with-param name="lineLength" select="72" />
  51.             </xsl:call-template>
  52.           </table>
  53.         </xsl:for-each>
  54.       </body>
  55.     </html>
  56.   </xsl:template>
  57.  
  58.   <xsl:template name="SplitFixedLengthToHTML">
  59.     <xsl:param name="strInput" select="''" />
  60.     <xsl:param name="lineLength" select="120" />
  61.  
  62.     <!-- This template recursively calls itself with the input string
  63.        each iteration process one line in the text file -->
  64.     <xsl:choose>
  65.       <xsl:when test="string-length($strInput) > $lineLength">
  66.         <tr>
  67.           <td>
  68.             <xsl:call-template name="TrimTrailing">
  69.               <xsl:with-param name="strInput" select="substring($strInput, 2, 10)" />
  70.             </xsl:call-template>
  71.           </td>
  72.           <td>
  73.             <xsl:call-template name="TrimTrailing">
  74.               <xsl:with-param name="strInput" select="substring($strInput, 12, 25)" />
  75.             </xsl:call-template>
  76.           </td>
  77.           <td>
  78.             <xsl:call-template name="TrimTrailing">
  79.               <xsl:with-param name="strInput" select="substring($strInput, 37, 21)" />
  80.             </xsl:call-template>
  81.           </td>
  82.           <td>
  83.             <xsl:call-template name="TrimTrailing">
  84.               <xsl:with-param name="strInput" select="substring($strInput, 58, 14)" />
  85.             </xsl:call-template>
  86.           </td>
  87.         </tr>
  88.         <xsl:call-template name="SplitFixedLengthToHTML">
  89.           <!-- Here, I am calling the template recursively with the same string
  90.            less one line -->
  91.           <xsl:with-param name="strInput" select="substring($strInput, $lineLength + 1)" />
  92.           <xsl:with-param name="lineLength" select="$lineLength" />
  93.         </xsl:call-template>
  94.       </xsl:when>
  95.     </xsl:choose>
  96.   </xsl:template>
  97.  
  98.   <xsl:template name="TrimTrailing">
  99.     <!--This template will recursively trim the trailing spaces from a string-->
  100.     <xsl:param name="strInput" select="''" />
  101.     <xsl:variable name="strLen" select="string-length($strInput)" />
  102.     <xsl:variable name="strOutput" select="substring($strInput, 1, $strLen - 1 )" />
  103.     <xsl:variable name="strLastChar" select="substring($strInput, $strLen, 1 )" />
  104.     <xsl:choose>
  105.       <xsl:when test="$strLastChar = ' '">
  106.         <xsl:call-template name="TrimTrailing">
  107.           <xsl:with-param name="strInput" select="$strOutput" />
  108.         </xsl:call-template>
  109.       </xsl:when>
  110.       <xsl:otherwise>
  111.         <xsl:value-of select="$strInput" />
  112.       </xsl:otherwise>
  113.     </xsl:choose>
  114.   </xsl:template>
  115. </xsl:stylesheet>